package demo.array;

import java.io.*;
import java.util.concurrent.ForkJoinPool;

/**
 * @ClassName : SpareseArray
 * @Description : 稀疏数组
 * @Author : JB
 * @Date: 2020-08-15 23:51
 */


public class SpareseArray {
    public static void main(String[] args) throws IOException {
          int rowNum = 11;
          int columnNum = 11;
        //创建一个二维数组表示棋盘 0表示没有棋子  1表示黑子 2表示红子
        int chessArray[][] = new int[rowNum][columnNum];
        chessArray[1][2] = 1;
        chessArray[2][3] = 2;
        chessArray[4][5] = 2;
        //输出二维数组
        System.out.println("原始二维数组：");
        for(int[] row:chessArray){
            for(int data:row){
                System.out.printf("%d ",data);
            }
            System.out.println();
        }
        //遍历得到数据规模
        int sum = 0;
        for(int[] row:chessArray){
            for(int data:row){
                if(data != 0){
                    sum++;
                }
            }
        }
        //将二维数组转化成稀疏数组
        int sparseArray[][] = new int[sum+1][3];
        sparseArray[0][0] = rowNum;
        sparseArray[0][1] = columnNum;
        sparseArray[0][2] = sum;
        int count = 0;
        for (int i = 0; i < rowNum; i++) {
            for (int j = 0; j < columnNum; j++) {
                if(chessArray[i][j] != 0 ){
                    count++;
                    sparseArray[count][0] = chessArray[i][j];
                    sparseArray[count][1] = i;
                    sparseArray[count][2] = j;
                }
            }
        }
        //输出稀疏数组
        System.out.println("稀疏数组：");
        for(int[] row:sparseArray){
            for(int data:row){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
        //将稀疏数组恢复
        int changeArray[][] = new  int[sparseArray[0][0]][sparseArray[0][1]];
        for (int i = 1; i <= sparseArray[0][2] ; i++) {
            changeArray[sparseArray[i][1]][sparseArray[i][2]] = sparseArray[i][0];
        }
        //输出转换后的数组
        System.out.println("输出转换后的数组");
        for(int[] row:changeArray){
            for(int data:row){
                System.out.printf("%d ",data);
            }
            System.out.println();
        }

        System.out.println(sparseArray.length); //二维数组的行
        System.out.println(sparseArray[0].length);//二维数组的列

        System.out.println("---------------");
        int[][] ints = changeArrayToSparseArray(chessArray);
        //输出转换后的数组
        System.out.println("输出转换后的数组");
        for(int[] row:ints){
            for(int data:row){
                System.out.printf("%d ",data);
            }
            System.out.println();
        }

        saveSparse(sparseArray);
        System.out.println("-----");
        int[][] ints1 = readFile();
        //输出文件中转换后的数组
        System.out.println("输出文件中转换后的数组");
        for(int[] row:ints1){
            for(int data:row){
                System.out.printf("%d ",data);
            }
            System.out.println();
        }

    }


    /**
     * 将二维数组转化成稀疏数组
     * @param chessArray
     * @return
     */
    public static int[][] changeArrayToSparseArray(int[][] chessArray){
        //遍历得到数据规模
        int sum = 0;
        for(int[] row:chessArray){
            for(int data:row){
                if(data != 0){
                    sum++;
                }
            }
        }
        //将二维数组转化成稀疏数组
        int sparseArray[][] = new int[sum+1][3];
        int rowNum = chessArray.length;//行
        int columnNum = chessArray[0].length;//列
        sparseArray[0][0] = rowNum;
        sparseArray[0][1] = columnNum;
        sparseArray[0][2] = sum;
        int count = 0;
        for (int i = 0; i < rowNum; i++) {
            for (int j = 0; j < columnNum; j++) {
                if(chessArray[i][j] != 0 ){
                    count++;
                    sparseArray[count][0] = chessArray[i][j];
                    sparseArray[count][1] = i;
                    sparseArray[count][2] = j;
                }
            }
        }
        return sparseArray;
    }

    /**
     * 将二维数组转化成稀疏数组,并保存到文件
     * @param chessArray
     */
    public static void saveSparse(int[][] chessArray) throws IOException {

        String str = "";
        for (int[] row:chessArray){
            for (int data: row){
                str+=data+",";
            }
            str+="-";
        }
        OutputStream outputStream = new FileOutputStream("E:\\test.txt");
        OutputStreamWriter out = new OutputStreamWriter(outputStream);
        try {
            out.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           out.flush();
           out.close();
        }
    }

    public static  int[][] readFile() throws FileNotFoundException {
        InputStream inputStream = new FileInputStream("E:\\test.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        StringBuffer lineBuffer = new StringBuffer();
        try {
            BufferedReader br = new BufferedReader(inputStreamReader); // 建立一个对象，它把文件内容转成计算机能读懂的语言
            String line = "";

            while ((line =br.readLine()) != null) {
                lineBuffer.append(line); // 一次读入一行数据
            }
            System.out.println(lineBuffer);

        } catch (IOException e) {
            e.printStackTrace();
        }
        String[] split = lineBuffer.toString().split("-");
        int columnNum = 0;
        int rowNum = 0;
        for(String str: split){
            columnNum = str.split(",").length;
            rowNum++;
        }
        int[][] resultArray = new int[rowNum][columnNum];

        for (int i = 0; i < split.length; i++) {
            for (int j = 0; j < split[i].split(",").length; j++) {
                resultArray[i][j] = Integer.valueOf(split[i].split(",")[j]);
            }
        }
        return resultArray;
    }
}
